package com.gable.exhibition.service.impl;

import com.gable.common.util.MoneyFormatUtil;
import com.gable.common.util.ReturnResultUtil;
import com.gable.common.util.UUIDGenerator;
import com.gable.exhibition.dao.*;
import com.gable.exhibition.model.Contract;
import com.gable.exhibition.model.ContractDevice;
import com.gable.exhibition.model.ContractDeviceEstimateSend;
import com.gable.exhibition.model.User;
import com.gable.exhibition.service.ContractService;
import com.gable.exhibition.service.SequenceService;
import com.gable.exhibition.util.MessageUtil;
import com.gable.exhibition.view.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mj on 2018/2/9.
 */
@Service
public class ContractServiceImpl implements ContractService {

    @Autowired
    ContractDao contractDao;

    @Autowired
    ContractDeviceDao contractDeviceDao;

    @Autowired
    ContractEstimateSendDao contractEstimateSendDao;

    @Autowired
    ContractDeviceEstimateSendDao contractDeviceEstimateSendDao;

    @Autowired
    SequenceService sequenceService;

    @Override
    public ReturnResultUtil contractList(ParameterView parameterView) {

        int count = contractDao.contractListCount(parameterView);
        List<ParameterView> contract = contractDao.contractList(parameterView);
        Map map = new HashMap();
        map.put("count",count);
        map.put("list",contract);
        return new ReturnResultUtil(200, MessageUtil.QUERYSUCCESS,map);
    }

    @Override
    public ReturnResultUtil deviceByContract(ParameterView parameterView) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        ContractView contractView = contractDeviceDao.deviceByContract(parameterView);
//        Integer whetherNeedInstall = contractView.getWhetherNeedInstall();
//        //发货单下有需要安装的设备，需要带出维修商列表
//        if(whetherNeedInstall > 0){
//            Long secondParty = contractView.getSecondParty();   //厂商ID
//            List<ManufacturerMaintainerView> manufacturerMaintainers = manufacturerMaintainerDao.getListByManufacturerId(secondParty);
//            contractView.setManufacturerMaintainers(manufacturerMaintainers);
//        }
        Long aLong = sequenceService.querySequence();
        String supplierNo = contractView.getSupplierNo()==null?"000000":contractView.getSupplierNo();
        String sendNo = supplierNo+sdf.format(new Date())+aLong;
        contractView.setSendNo(sendNo);
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,contractView);
    }

    @Override
    public ReturnResultUtil querycontractInfo(Long id) {
        ContractView contractView = contractDao.querycontractInfo(id);
        contractView.setContractAmountStr(MoneyFormatUtil.MoneyToString(contractView.getContractAmount()));
        for(ContractDeviceView contractDeviceView : contractView.getContractDeviceList()){
            contractDeviceView.setPriceStr(MoneyFormatUtil.MoneyToString(contractDeviceView.getPrice()));
        }
        List<ContractDeviceEstimateActual> list = contractDao.queryContractDeviceEstimateActual(id);
        contractView.setEstimateActualList(list);
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,contractView);
    }

    @Override
    public ReturnResultUtil estimateQuantity(Long estimateId) {
        List<ContractDeviceView> list = contractDeviceDao.estimateQuantity(estimateId);
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,list);
    }

    @Override
    public ReturnResultUtil deviceEstimateActual(Long contractDeviceId) {
        List<ContractDeviceView> list = contractDeviceDao.deviceEstimateActual(contractDeviceId);
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,list);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ReturnResultUtil saveContractByExcel(List<ImportExcelContractModel> list, User user) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int count = 0;
        for(ImportExcelContractModel ic : list){
            Contract contract = new Contract();
            contract.setContractId(UUIDGenerator.getUniqueLong());
            contract.setContractNo(ic.getContractNo());
            contract.setContractName(ic.getContractName());
            contract.setRemark(ic.getContractRemark());
            contract.setContractAmount(ic.getContractAmount());
            contract.setContractType(ic.getContractType());
            contract.setCreateTime(df.format(new Date()));
            contract.setCreatePerson(user.getUserId());
            contract.setDeviceWarrantyPeriod(ic.getDeviceWarrantyPeriod());
            contract.setFirstParty(ic.getHospitalId());
            contract.setFirstPartyContacts(ic.getHospitalContacts());
            contract.setFirstPartyPhone(ic.getHospitalPhone());
            contract.setIsDel(0);
            contract.setSecondParty(ic.getSupplierId());
            contract.setSecondPartyContacts(ic.getSupplierContacts());
            contract.setSecondPartyPhone(ic.getSupplierPhone());
            contract.setSignDate(ic.getSignDate());
            int result = contractDao.insertSelective(contract);
            if(result > 0){
                for(ContractDevice cd  : ic.getContractDeviceList()){
                    cd.setIsDel(0);
                    cd.setContractDeviceId(UUIDGenerator.getUniqueLong());
                    cd.setContractId(contract.getContractId());
                    cd.setActualQuantity(0);
                    contractDeviceDao.insertSelective(cd);
                }
                count++;
            }
        }

        if(count != list.size()){
            ContractDevice c = null;
            c.getUpkeepCycle();
        }
        return new ReturnResultUtil(200,MessageUtil.SAVESUCCESS);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ReturnResultUtil saveEstimateByExecl(List<ContractEstimateSendView> cList, User user) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int count = 0;
        for(ContractEstimateSendView c : cList){
            c.setContractEstimateSendId(UUIDGenerator.getUniqueLong());
            c.setIsDel(0);
            c.setCreateTime(df.format(new Date()));
            c.setCreatePerson(user.getUserId());
            int result = contractEstimateSendDao.insertSelective(c);
            if(result > 0){
                count ++;
                if(c.getContractEstimateSendList() != null){
                    for(ContractDeviceEstimateSend d : c.getContractEstimateSendList()){
                        d.setContractDeviceEstimateSendId(UUIDGenerator.getUniqueLong());
                        d.setContractEstimateSendId(c.getContractEstimateSendId());
                        d.setCreateTime(df.format(new Date()));
                        d.setCreatePerson(user.getUserId());
                        d.setActualQuantity(0);
                        d.setIsDel(0);
                        contractDeviceEstimateSendDao.insertSelective(d);
                    }
                }
            }
        }
        if(count != cList.size()){
            ContractDeviceEstimateSend s = null;
            s.getContractDeviceId();
        }
        return new ReturnResultUtil(200,MessageUtil.SAVESUCCESS);
    }
}
